/*
 * @Description: 
 * @Author: Zhang Zhangda
 * @Date: 2021-03-04 10:14:07
 * @LastEditTime: 2021-03-09 14:53:34
 * @LastEditors: Zhang Zhaoda
 */
import { Effect, ImmerReducer, Subscription } from 'umi';
//引入接口
import {
  getQuestionType,
  addType,
  whatchAllQuestion,
  getAllQuestionsType,
  getSubject,
  addQuestion,
  upDateQuestion,
  serarchByCondition
} from "@/services/excal.request";
import {getCookie} from "@/utiles/Cookie"
//试题分类
export interface QuestionsTypeInter {
  questions_type_id: string;
  questions_type_text: string;
  questions_type_sort: number
}
//所有试题类型
export interface allQuestionsTypeInter {
  exam_id: string;
  exam_name: string
}
//所有试题
export interface allSubjectInter {
  subject_id: string;
  subject_text: string
}

//查看试题
export interface QuestionsInter {
  exam_id: string;
  exam_name: string;
  json_path: string;
  questions_answer: string;
  questions_id: string;
  questions_stem: string;
  questions_type_id: string;
  questions_type_text: string;
  subject_id: string;
  subject_text: string;
  title: string;
  user_id: string;
  user_name: string;
}
//格式化查看试题
export interface FormatQuestionsInter{
  title:string;
  list:QuestionsInter[]
}

//状态类型
export interface ExcalModelState {
  QuestionsType: QuestionsTypeInter[];
  allQuestionsType: allQuestionsTypeInter[];
  allSubject: allSubjectInter[];
  Questions: QuestionsInter[];
  isAddOk: number;
  isChangeOk:number;
  keys:string[];
}
interface text extends ImmerReducer<ExcalModelState> {

}
export interface IndexModelType {
  namespace: 'excal';
  state: ExcalModelState;
  effects: {
    getQuestionsType: Effect;//获取试题分类
    addQuextionsType: Effect;//添加试题分类
    watchQuestions: Effect;//查看试题
    getAllQuestionType: Effect;//获取所有考试类型
    getAllSubject: Effect;//获取所有试题
    addQuestions: Effect;//添加试题
    upDateQuestions:Effect;//更新试题
    changeTab:Effect
  };
  reducers: {
    saveQuestionsType: text;//保存试题分类
    saveAllQuestions: text;//保存所有试题
    saveAllQuestionsType: text;//保存所有考试类型
    saveAllSubject: text;//获取所有试题
    saveIsAddOk: text//是否添加试题成功
    saveIsChangeOk:text;//是否改变试题成功
    saveTab:text
    // getData:text;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'excal',

  state: {
    QuestionsType: [],
    allQuestionsType: [],
    allSubject: [],
    Questions: [],
    isAddOk: -1,
    isChangeOk:-1,
    keys:[],
  },
  //相当于vue中actions
  effects: {
    //payload传参,put触发reducers,call调用接口
    //请求所有类型
    *getQuestionsType({ payload }, { call, put }) {
      //请求
      let result = yield call(getQuestionType)
      if (result.code === 1) {

        yield put({
          type: 'saveQuestionsType',
          data: result.data
        })

      }
    },
    //添加试卷类型
    *addQuextionsType({ payload }, { call, put }) {
      //请求
      let result = yield call(addType, payload)
      if (result.code === 1) {
        yield put({
          type: 'getQuestionsType'
        })
      }
    },

    //查看试题
    *watchQuestions({ payload }, { call, put }) {
      let result = yield call(whatchAllQuestion)

      if (result.code === 1) {
        //格式化数据
        let keys:string[]=[...new Set(result.data.map((item:QuestionsInter)=>item.subject_text))] as string[];
        yield put({
          type:'saveAllQuestions',
          data:result.data,
          keys
        })

      }
     
    },
    //获取所有考试类型
    *getAllQuestionType({ payload }, { call, put }) {
      let result = yield call(getAllQuestionsType)
      if (result.code === 1) {
        yield put({
          type: 'saveAllQuestionsType',
          data: result.data
        })
      }
    },
    //获取所有试题
    *getAllSubject({ payload }, { call, put }) {
      
      let result = yield call(getSubject)
      if (result.code === 1) {
        yield put({
          type: 'saveAllSubject',
          data: result.data
        })

      }
    },
    //添加考试
    *addQuestions({ payload }, { call, put }) {
      
      let result = yield call(addQuestion, payload)
      if (result.code === 1) {
        yield put({
          type: 'saveIsAddOk',
          data:1
        })
        yield put( {
          type:'saveAllQuestionsType'
        })
      }else{
        yield put({
          type: 'saveIsAddOk',
          data:0
        })

      }
    },
    //更新试题
    *upDateQuestions({ payload }, { call, put }){
      
      let result=yield call(upDateQuestion,payload)
      
      if (result.code === 1) {
      
        yield put({
          type: 'saveIsChangeOk',
          data:1
        })
      }else{
        yield put({
          type: 'saveIsChangeOk',
          data:0
        })
      }
      
    },
    *changeTab({ payload }, { call, put}){
      let result =yield call(serarchByCondition,payload)
      if(result.code===1){
        
        yield put({
          type:'saveTab',
          data:result.data
        })
      }
    }

  },
  //相当于vue的mutations
  reducers: {

    // 启用 immer 之后

    //保存试题分类数据
    saveQuestionsType(state, action) {

      state.QuestionsType = action.data;
    },
    //保存所有试题
    saveAllQuestions(state, action) {

      state.Questions = action.data;
      state.keys=action.keys
    },
    //保存所有试题类型
    saveAllQuestionsType(state, action) {
      state.allQuestionsType = action.data
    },
    //保存所有试题
    saveAllSubject(state, action) {
      state.allSubject = action.data

    },
    //是否添加成功
    saveIsAddOk(state, action) {
      state.isAddOk = action.data
    },
    //是否改变成功
    saveIsChangeOk(state, action) {
      state.isChangeOk = action.data
    },
    saveTab(state,action){
      state.Questions = action.data;
      
    }
    

  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        let token=getCookie('token');
        if(token){
          if (pathname ==='/index/showexcal'||pathname ==="/index/addexcal"||pathname ==='/indexexcallist') {
          
            dispatch({
              type: 'getAllQuestionType'
            })
            dispatch({
              type: 'getAllSubject'
            })
            dispatch({
              type: 'getQuestionsType',
            })
  
          }
          //查看试题
          if (pathname === '/index/showexcal') {
            dispatch({
              type: 'watchQuestions'
            })
          }
        }
        
      });
    }
  }
};

export default IndexModel;
